package club.zhilin.guli.service.ucenter.controller.portal;


import club.zhilin.guli.common.base.domain.JwtInfo;
import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.common.base.result.CommonResultCodeEnum;
import club.zhilin.guli.common.base.util.JwtUtil;
import club.zhilin.guli.service.base.dto.MemberDTO;
import club.zhilin.guli.service.base.exception.GlobalException;
import club.zhilin.guli.service.ucenter.domain.vo.LoginVO;
import club.zhilin.guli.service.ucenter.domain.vo.RegisterVO;
import club.zhilin.guli.service.ucenter.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zhilin
 * @since 2022-02-24 17:34:48
 */
@Slf4j
@Api(tags = "会员管理")
@RestController
@RequestMapping("/portal/ucenter/member")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PortalMemberController {

    private final IMemberService iMemberService;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public CommonResult<String> register(@RequestBody RegisterVO registerVO) {
        return iMemberService.register(registerVO);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult<String> login(@RequestBody LoginVO loginVO) {
        return iMemberService.login(loginVO);
    }

    @ApiOperation("根据token获取登录信息")
    @GetMapping("/getLoginInfo")
    public CommonResult<JwtInfo> getLoginInfo(HttpServletRequest request) {
        try {
            return JwtUtil.requestGetMember(request);
        } catch (Exception e) {
            log.error("解析用户信息失败", e);
            throw new GlobalException(CommonResultCodeEnum.FETCH_USERINFO_ERROR);
        }
    }

    @ApiOperation("根据会员id查询会员信息")
    @GetMapping("/inner/member/{memberId}")
    public CommonResult<MemberDTO> selectMemberByMemberId(@PathVariable("memberId") String memberId) {
        return iMemberService.selectMember(memberId);
    }
}

